/**
 * 
 */
package testcases;

import java.io.BufferedWriter;
import java.io.FileWriter;
import java.util.Calendar;
import java.util.Date;

import org.junit.AfterClass;
import org.junit.BeforeClass;
import org.junit.Test;

import com.trentech.billcalc.BillCalc;
import com.trentech.billcalc.FinanceManager;


/**
 * @author marieputri
 *
 */
public class MainTest {

	@BeforeClass
	public static void setFirst()
	{
		Calendar cl = Calendar.getInstance();
    	ms = cl.getTimeInMillis();
    	
    	new EventData();
	}
	
	@AfterClass
	public static void setAfter() 
	{
		Calendar cl2 = Calendar.getInstance();
		System.out.println("\nExecution time : " + (cl2.getTimeInMillis() - ms));
	}
	
	@Test
	public void testPatch1() throws Exception
	{
		// Coverage : 0,2 + 58,5 + 69,8 : 42,83
		
		startFile();
		FinanceManager fm = new FinanceManager();
		AddCompareDataTest.AddEvents(fm);
	}
	
	@Test 
	public void testPatch2() throws Exception
	{
		// Coverage : 0,2 + 62,6 + 69,9 : 44,23
		
		startFile();
		FinanceManager fm = new FinanceManager();
		AddCompareDataTest.AddEvents(fm);
		
		AddCompareDataTest.Compare_SetupBill(fm, EventData.ListSetupBill.get(0));
	}
	
	@Test
	public void testPatch3() throws Exception
	{
		// Coverage : 0,2 + 72,6 + 69,8 : 47,53
		
		startFile();
		FinanceManager fm = new FinanceManager();
		AddCompareDataTest.AddEvents(fm);
		
		AddCompareDataTest.Compare_SetupBill(fm, EventData.ListSetupBill.get(0));
		AddCompareDataTest.Compare_StartLease(fm, EventData.ListStartLease.get(1));
		AddCompareDataTest.Compare_Bill(fm, EventData.ListBill.get(0));
		AddCompareDataTest.Compare_Pay(fm, EventData.ListPay.get(0));
	}
	
	@Test
	public void testPatch4() throws Exception
	{
		// Coverage : 0,2 + 78,7 + 69,8 : 49,56
		
		startFile();
		FinanceManager fm = new FinanceManager();
		AddCompareDataTest.AddEvents(fm);
		
		AddCompareDataTest.Compare_SetupBill(fm, EventData.ListSetupBill.get(0));
		AddCompareDataTest.Compare_StartLease(fm, EventData.ListStartLease.get(1));
		AddCompareDataTest.Compare_Bill(fm, EventData.ListBill.get(0));
		AddCompareDataTest.Compare_Pay(fm, EventData.ListPay.get(0));
		AddCompareDataTest.Compare_OptOut(fm, EventData.ListOptOut.get(0));
		AddCompareDataTest.Compare_OptIn(fm, EventData.ListOptIn.get(0));
		AddCompareDataTest.Compare_TransferLease(fm, EventData.ListTransferLease.get(0));
	}
	
	@Test
	public void testPatch5() throws Exception
	{
		// Coverage : 0,2 + 81,8 + 70,7 : 50,9
		
		startFile();
		FinanceManager fm = new FinanceManager();
		AddCompareDataTest.AddEvents(fm);
		AddCompareDataTest.CompareEvents(fm);
		GUITest.TestMessages();
	}

	@Test 
	public void testPatch6() throws Exception
	{
		// Coverage : 43,4 + 54,2 + 62,3 : 53,3
		
		startFile();
		FinanceManager fm = new FinanceManager();
		AddCompareDataTest.SetupBill_Add(fm);
		AddCompareDataTest.Arrive_Add(fm);
		AddCompareDataTest.StartLease_Add(fm);
		AddCompareDataTest.Bill_Add(fm);
		AddCompareDataTest.Leave_Add(fm);
		AddCompareDataTest.Pay_Add(fm);
		AddCompareDataTest.OptOut_Add(fm);
		AddCompareDataTest.OptIn_Add(fm);
		GUITest.TestMessages();
		GUITest.TestBillCalcFrame(fm);
	}
	
	@Test
	public void testPatch7() throws Exception
	{
		// Coverage : 43,4 + 62,9 + 60 : 55,43
		
		startFile();
		FinanceManager fm = new FinanceManager();
		
		AddCompareDataTest.SetupBill_Add(fm);
		AddCompareDataTest.Arrive_Add(fm);
		AddCompareDataTest.StartLease_Add(fm);
		AddCompareDataTest.Bill_Add(fm);
		AddCompareDataTest.Leave_Add(fm);
		AddCompareDataTest.Pay_Add(fm);
		
		AddCompareDataTest.Compare_SetupBill(fm, EventData.ListSetupBill.get(0));
        AddCompareDataTest.Compare_StartLease(fm, EventData.ListStartLease.get(1));
		AddCompareDataTest.Compare_Bill(fm, EventData.ListBill.get(0));
        AddCompareDataTest.Compare_Pay(fm, EventData.ListPay.get(0));
		
		GUITest.TestMessages();
		GUITest.TestBillCalcFrame(fm);
	}
	
	@Test
	public void testPatch8() throws Exception
	{
		// Coverage : 43,5 + 71,6 + 57 : 57,36
		
		startFile();
		FinanceManager fm = new FinanceManager();
		
		AddCompareDataTest.SetupBill_Add(fm);
		AddCompareDataTest.Arrive_Add(fm);
		AddCompareDataTest.StartLease_Add(fm);
		AddCompareDataTest.Bill_Add(fm);
		AddCompareDataTest.Leave_Add(fm);
		AddCompareDataTest.OptIn_Add(fm);
		
		AddCompareDataTest.CompareEvents(fm);
		
		GUITest.TestMessages();
		GUITest.TestBillCalcFrame(fm);
	}
	
	@Test
	public void testPatch9() throws Exception
	{
		// Coverage : 42,6 + 71,2 + 65,6 : 59,8
		
		startFile();
		FinanceManager fm = new FinanceManager();
		
		AddCompareDataTest.SetupBill_Add(fm);
		AddCompareDataTest.Arrive_Add(fm);
		AddCompareDataTest.StartLease_Add(fm);
		AddCompareDataTest.Bill_Add(fm);
		AddCompareDataTest.TransferLease_Add(fm);
		AddCompareDataTest.StartVacation_Add(fm);
		
		AddCompareDataTest.CompareEvents(fm);
		
		GUITest.TestBillCalcFrame(fm);
	}
	
	@Test
	public void testPatch10() throws Exception
	{
		// Coverage : 66,4 + 79,1 + 42,9 : 62,8
		
		startFile();
		FinanceManager fm = new FinanceManager();
		
		AddCompareDataTest.SetupBill_Add(fm);
		AddCompareDataTest.Arrive_Add(fm);
		AddCompareDataTest.Bill_Add(fm);
		AddCompareDataTest.OptIn_Add(fm);
		AddCompareDataTest.OptOut_Add(fm);
		
		AddCompareDataTest.CompareEvents(fm);
		GUITest.TestGUIComponents(fm);
		OtherTest.TestOtherComponents(fm);
	}
	
	@Test
	public void testPatch11() throws Exception
	{
		// Coverage : 34,5 + 85,7 + 72,3 : 64,16
		
		startFile();
		FinanceManager fm = new FinanceManager();
		
		AddCompareDataTest.AddEvents(fm);
		AddCompareDataTest.CompareEvents(fm);
		
		OtherTest.TestOtherComponents(fm);
	}
	
	@Test
	public void testPatch12() throws Exception
	{
		// Coverage : 64,3 + 64,1 + 71,2 : 66,5
		
		startFile();
		FinanceManager fm = new FinanceManager();
		
		AddCompareDataTest.SetupBill_Add(fm);
		AddCompareDataTest.Arrive_Add(fm);
		AddCompareDataTest.StartLease_Add(fm);
		AddCompareDataTest.Bill_Add(fm);
		AddCompareDataTest.Pay_Add(fm);
		AddCompareDataTest.OptOut_Add(fm);
		AddCompareDataTest.TransferLease_Add(fm);
		AddCompareDataTest.StartVacation_Add(fm);

		GUITest.TestBillCalcFrame(fm);
		GUITest.TestEventEditor(fm);
	}
	
	@Test
	public void testPatch13() throws Exception
	{
		// Coverage : 65,2 + 76,5 + 67,7 : 69,8
		
		startFile();
		
		PayTest.testPay();
		
		FinanceManager fm = new FinanceManager();
		AddCompareDataTest.Arrive_Add(fm);
		AddCompareDataTest.StartLease_Add(fm);
		AddCompareDataTest.CompareEvents(fm);
		
		GUITest.TestBillCalcFrame(fm);
		GUITest.TestEventEditor(fm);
	}
	
	@Test
	public void testPatch14() throws Exception
	{
		// Coverage : 65,2 + 70,8 + 79,7 : 71,9
		
		startFile();
		
		PayTest.testPay();
		
		FinanceManager fm = new FinanceManager();
		AddCompareDataTest.Arrive_Add(fm);
		AddCompareDataTest.StartLease_Add(fm);
		AddCompareDataTest.SetupBill_Add(fm);
		AddCompareDataTest.Bill_Add(fm);
		AddCompareDataTest.Leave_Add(fm);
		AddCompareDataTest.Pay_Add(fm);
		AddCompareDataTest.OptOut_Add(fm);
		AddCompareDataTest.OptIn_Add(fm);
		AddCompareDataTest.TransferLease_Add(fm);
		AddCompareDataTest.RenewLease_Add(fm);
		AddCompareDataTest.StartVacation_Add(fm);
		AddCompareDataTest.StopVacation_Add(fm);
		
		GUITest.TestBillCalcFrame(fm);
		GUITest.TestEventEditor(fm);
	}
	
	@Test
	public void testPatch15() throws Exception
	{
		// Coverage : 65,2 + 81,1 + 73,8 : 73,36
		
		startFile();
		LeaseTest.testLease();
		
		startFile();
		FinanceManager fm = new FinanceManager();
		AddCompareDataTest.AddEvents(fm);
		AddCompareDataTest.Compare_SetupBill(fm, EventData.ListSetupBill.get(0));
		AddCompareDataTest.Compare_StartLease(fm, EventData.ListStartLease.get(1));
		AddCompareDataTest.Compare_Bill(fm, EventData.ListBill.get(0));
		AddCompareDataTest.Compare_Pay(fm, EventData.ListPay.get(0));
		
		GUITest.TestBillCalcFrame(fm);
		GUITest.TestEventEditor(fm);
	}
	
	@Test
	public void testPatch16() throws Exception
	{
		// Coverage : 65,2 + 88,6 + 73,8 : 75,86
		
		startFile();
		LeaseTest.testLease();
		
		startFile();
		FinanceManager fm = new FinanceManager();
		AddCompareDataTest.AddEvents(fm);
		AddCompareDataTest.CompareEvents(fm);
		
		GUITest.TestBillCalcFrame(fm);
		GUITest.TestEventEditor(fm);
	}
	
	@Test
	public void testPatch17() throws Exception
	{
		// Coverage : 65,2 + 88,6 + 79,8 : 77,86
		
		startFile();
		LeaseTest.testLease();
		
		startFile();
		FinanceManager fm = new FinanceManager();
		AddCompareDataTest.AddEvents(fm);
		AddCompareDataTest.CompareEvents(fm);
		
		GUITest.TestBillCalcFrame(fm);
		GUITest.TestEventEditor(fm);

		startFile();
    	PayTest.testPay();
		
	}
	
	@Test
	public void testPatch18() throws Exception
	{
		// Coverage : 66,5 + 92,0 + 83,4 : 79,53
		
		startFile();
		LeaseTest.testLease();
		
		startFile();
    	PayTest.testPay();
    	
    	startFile();
		FinanceManager fm = new FinanceManager();
		AddCompareDataTest.AddEvents(fm);
		AddCompareDataTest.CompareEvents(fm);
		
		GUITest.TestGUIComponents(fm);
		OtherTest.TestOtherComponents(fm);
	}
	
	@Test
	public void testPatch19() throws Exception
	{
		// Coverage : 66,6 + 85,1 + 92,1 : 81,26
		
		startFile();
		LeaseTest.testLease();
		
		startFile();
    	PayTest.testPay();
		
		startFile();
		FinanceManager fm = new FinanceManager();
		AddCompareDataTest.AddEvents(fm);
		AddCompareDataTest.Compare_SetupBill(fm, EventData.ListSetupBill.get(0));
		AddCompareDataTest.Compare_StartLease(fm, EventData.ListStartLease.get(1));
		AddCompareDataTest.Compare_Bill(fm, EventData.ListBill.get(0));
		AddCompareDataTest.Compare_Pay(fm, EventData.ListPay.get(0));
		AddCompareDataTest.Compare_OptOut(fm, EventData.ListOptOut.get(0));
				
		GUITest.TestGUIComponents(fm);
		OtherTest.TestOtherComponents(fm);
		BillCalc.main(null);
	}
	
	@Test
	public void testPatch20() throws Exception
	{
		// Coverage : 66,6 + 90,7 + 92,1 : 83,13
		
		startFile();
		LeaseTest.testLease();
		
		startFile();
    	PayTest.testPay();
		
		startFile();
		FinanceManager fm = new FinanceManager();
		AddCompareDataTest.AddEvents(fm);
		AddCompareDataTest.CompareEvents(fm);
		
		GUITest.TestGUIComponents(fm);
		OtherTest.TestOtherComponents(fm);
		BillCalc.main(null);
	}

	@Test
	public void testPatch21() throws Exception
	{
		// Coverage : 70,2 + 93,6 + 93,8 : 85,86
		
		startFile();
		LeaseTest.testLease();
		
		startFile();
    	PayTest.testPay();
		
		startFile();
		FinanceManager fm = new FinanceManager();
		AddCompareDataTest.AddEvents(fm);
		AddCompareDataTest.CompareEvents(fm);
		
		GUITest.TestGUIComponents(fm);
		OtherTest.TestOtherComponents(fm);
		BillCalc.main(null);
		OtherTest.TestOthers(fm);		
	}
	
	
    private static long ms;
    public static boolean isOriginal = true;
    public static boolean isFaultFound = true;
    
    public static Date addDays(int Add) {
        // static function to return date "Add" days from today
        
        Integer Add_ = new Integer(Add);
        long det = Add_.longValue() * 86400 * 1000;
        Calendar cl = Calendar.getInstance();
        cl.setTimeInMillis(ms + det);

        return cl.getTime();
    }
    
    public static void startFile() throws Exception {
        // static procedure to rewrite events.xml into an empty xml
        
        String write = "<?xml version=\"1.0\" encoding=\"UTF-8\"?> \n"
                + "<java version=\"1.6.0_23\" class=\"java.beans.XMLDecoder\"> \n"
                + "<object class=\"java.util.ArrayList\"> \n" + "</object> \n"
                + "</java> \n";

        FileWriter fstream = new FileWriter("events.xml");
        BufferedWriter out = new BufferedWriter(fstream);

        out.write(write);
        out.close();
        fstream.close();
    }
}


// consider about using static procedure